<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {

    public function register(){
        //添加用户
        //用户名是否存在，口令是否正确，口令加密
        $userModel=D('Users');

        if($userModel->create()){
            if($_FILES['photo']['error']==0){//判断文件上传是否成功
                $fileName=$this->img();
                $userModel->photo=$fileName;//获取并设置文件名称
            }
            if($userModel->add()){
                $this->success('用户注册成功！',U('Admin/Index/users'));
            }else{
                $this->error('用户注册失败！');
            }
        }else{
            $this->error($userModel->getError());
        }
    }
    public function upload(){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 3145728 ;// 设置附件上传大小
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Public/Uploads/photo/'; // 设置附件上传根目录
        //$upload->savePath = ''; // 设置附件上传（子）目录
        $upload->autoSub=false;
// 上传文件
        $info = $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $this->error($upload->getError());
        }else{// 上传成功
            return $info;
        }
    }

    function img(){
        $photoinfo = $this->upload();
        $filePath='./Public/Uploads/photo/';
        $oldFileName=$photoinfo['photo']['savename'];
        $oldFileFullName=$filePath."\\".$oldFileName;
        $newFileName='thumb_'.$oldFileName;
        $newFileFullName=$filePath."\\".$newFileName;
        $image=new \Think\Image();
        $image->open($oldFileFullName);
        $image->thumb(100,100)->save($newFileFullName);
        return $newFileName;
    }
}